
const path = require('path');
const fs = require('fs');



class Melody {
    cache = {
        'a.js': {a:1}
    };   // { }
    require = (url) => {
        const targetUrl = path.resolve(__dirname, url);
        if(this.cache[targetUrl]){
            return this.cache[targetUrl];
        }
        const code = fs.readFileSync(targetUrl, 'utf-8');
        const reg = /melody.exports/g;
        const newCode = 
        `
        (function(){
            ${code.replace(reg, 'return ')}
        })()
        `
        const res =  eval(newCode);
        this.cache[targetUrl] = res;
        return res
    }
}


module.exports = new Melody();